local Item = import(".Item")
local Jewel = class("Jewel", Item)

local string_split = string.split

function Jewel:ctor()
    Jewel.super.ctor(self,Enums.ItemBagType.jewel)
    self.showType = Enums.ItemShowType.jewel

    self.bind = 0
    -- self.exp = 0
    self.state = Enums.JewelState.inBag
end

function Jewel:read( value )
    Jewel.super.read(self, value)

    self.bind = value.bind or 0
	-- self.exp = value.exp or 0
end

function Jewel:initDB()
    local conf = GD:queryJewelById(self.tid)
    assert(conf ~= nil, "Jewel.conf can't be nil")
    self.conf = conf

    self.quality = tonumber(conf.quality or 1)
    self.lv = tonumber(conf.lv or 1) -- 宝石本身等级
    self.useLv = tonumber(conf.equipLv or 1) -- 使用需求等级
    self.type = tonumber(conf.type or 0) -- 宝石类型( 进攻型/生存型 )
    self.bindType = tonumber(conf.bind or 0) -- 绑定类型
    self.gearScore = tonumber(conf.gearScore or 0) -- 宝石评分

    self.name = conf.name
    self.desc = conf.desc
    self.pos  = conf.pos
    self.posShow = conf.posShow

    self.orderLevel = 0 -- 排序等级
    self._smallIcon = nil
    self._smallIconBg = nil
    self._miniIcon = nil
    self._curJewelMaxLevel = nil -- 当前宝石可以强化到的最大等级
end

function Jewel:getIcon()
    if not self._icon then
       self._icon = string.format("%s%s.png", Res.jewelIconPath, self.conf.icon)
    end
    return self._icon
end

function Jewel:getDesc()
    if self.desc then
        local ps = self.desc:split("|")
        local pps = {}
        for i, v in ipairs(ps) do 
            pps[#pps+1] = L(v)
        end
        return string.format(ps[1], unpack(pps, 2))
    end

    return ""
end

-- 可装备位置描述
function Jewel:getPosDesc()
    return L("jewelInsertPosition") .. L(self.pos or "")
end

-- 可装备位置图标
function Jewel:getPosShowIconList()
    if not self._posShowIconList then
        local iconList = {}
        if self.posShow then
            local ary = string.split(self.posShow, "/")
            for i,v in ipairs(ary) do
                iconList[i] = string.format("art/jewel_icon/%s.png", v)
            end
        end
        self._posShowIconList = iconList
    end
    return self._posShowIconList
end

-- 是否能转换指定宝石 1. 品质相同 2.等级相同 3.职业相同
function Jewel:canTransformToJewel(jewel)
    if not jewel then return false end
    if jewel.tid == self.tid then return false end

    if jewel.quality == self.quality and jewel.lv == self.lv then
        local sourceClass = self:getClass()
        local targetClass = jewel:getClass()
        return sourceClass == "0" or targetClass == "0" or sourceClass == targetClass
    end
    return false
end

-- 转换需要消化的道具
function Jewel:getTransformCostItems()
    if not self._transformCostItems then
        local costItems = {}
        local conf = GD:getJewelTransformCostConf(self.quality, self.lv)
        local costStr = conf and conf.transferCost
        if costStr then
            costItems = UD:parseItemsFromString(costStr)
        end
        self._transformCostItems = costItems
    end
    return self._transformCostItems
end

-- 判定道具是否为宝石转换所需
function Jewel:isTransformCostItem(tid)
    local costItems = self:getTransformCostItems()
    for i,item in ipairs(costItems) do
        if item:getId() == tid then
            return true
        end
    end
    return true
end

-- 小icon
function Jewel:getSmallIcon()
    if not self._smallIcon then
        self._smallIcon = string.format("%s%s.png", Res.slotIconPath, self.conf.smallIcon)
    end
    return self._smallIcon
end

function Jewel:getSmallIconBg()
    if not self._smallIconBg then
        self._smallIconBg = string.format("%squality_%s.png", Res.slotIconPath, self.quality)
    end
    return self._smallIconBg
end

-- 迷你icon
function Jewel:getMiniIcon()
    if not self._miniIcon then
        self._miniIcon = string.format("%s%s.png", Res.slotIconPath, self.conf.miniIcon)
    end
    return self._miniIcon
end

function Jewel:getUid()
    return self.uid
end

function Jewel:getType()
    return self.type
end

function Jewel:getUseLv()
    return self.useLv
end

function Jewel:getAttrType()
    return tonumber(self.conf.attrType)
end

function Jewel:getAttrVal()
    return tonumber(self.conf.attrVal)
end

function Jewel:getQuality()
    return self.quality
end

function Jewel:getLevel()
    return self.lv
end

function Jewel:getLevelPlus1()
    return self.lv + 1
end

function Jewel:getGearScore()
    return self.gearScore
end

-- 取当前珠宝可以强化到的最大等级
function Jewel:getGrowLvCap()
    -- TODO 这种配置取法需要修改
    if not self._curJewelMaxLevel then
        local conf = GD:queryJewelLevelUpGrowLvCap(self:getClass(), self.type, self.quality, self:getAttrType())
        self._curJewelMaxLevel = tonumber(conf.lv) or 1
    end
    return self._curJewelMaxLevel
end

-- 是否可以分解？
function Jewel:isCanBreak()
    return self.conf.splitResource ~= nil
end

-- 是否可以自动分解
function Jewel:isCanAutoBreak()
    return self:isCanBreak() and self.quality <= 3
end

function Jewel:getBreakStr()
    return self.conf.splitResource
end

-- 是否可以继续强化( 升级 )
function Jewel:canContinueStrength()
    if self:getLevel() < self:getGrowLvCap() then
        return true
    end
    return false
end

function Jewel:getOrderLevel()
    return self.orderLevel
end
function Jewel:setOrderLevel( l )
    self.orderLevel = l
end

-- 宝石在 背包/装备 中
function Jewel:setState( s )
    self.state = s
end

-- 背包中
function Jewel:isInBag()
    return self.state == Enums.JewelState.inBag
end

-- 在装备中
function Jewel:isInEquip()
    return self.state == Enums.JewelState.inSlot
end

function Jewel:setEquipTid( tid )
    self.equipTid = tid
end

function Jewel:setEquipUid( uid )
    self.equipUid = uid
end

function Jewel:getGiveExp()
    return 0
    -- local giveExp = self.exp
    -- local _, _, decomNum = self:getBreakParams()
    -- if decomNum then
    --     giveExp = giveExp + decomNum
    -- end
    -- return giveExp
end

-- 来源
function Jewel:getResource()
    local resource = GD:getConvertResource(self.conf.getResource, Enums.ItemBagType.jewel, tonumber(self.conf.ID))
	return resource
end

return Jewel
